package com.yanxu.oauth2.server.service;

import com.yanxu.model.domain.SignInIdentity;
import com.yanxu.model.pojo.Diners;
import com.yanxu.oauth2.server.mapper.DinersMapper;
import com.yanxu.utils.AssertUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025-09-25-2025
 * @Description: 用户验证 service
 */
@Service
//public class UserService implements UserDetailsService {
public class UserService implements UserDetailsManager {
    /*
    * --- UserDetailsService
    * -------UserDetailsManager
    * ----------UserService (自定义的)
    * */



    /*
    * 在创建用户的时候，会调用该方法
    * */
    @Override
    public void createUser(UserDetails user) {

    }

    /*
     * 在更新用户的时候，会调用该方法
     * */
    @Override
    public void updateUser(UserDetails user) {

    }

    /*
     * 在删除用户的时候，会调用该方法
     * */
    @Override
    public void deleteUser(String username) {

    }
    /*
     * 在修改密码的时候，会调用该方
     * */
    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }

    @Resource
    private DinersMapper dinersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //前置校验
        AssertUtil.isNotEmpty(username,"请输入用户名!");

        //根据用户名，查询 diners 食客服务
        Diners diners = dinersMapper.selectByAccountInfo(username);
        if (diners == null) {
            throw new UsernameNotFoundException("用户名或密码错误，请重新输入");
        }

        //初始化登录认证对象
        SignInIdentity signInIdentity = new SignInIdentity();
        //拷贝属性
        BeanUtils.copyProperties(diners,signInIdentity);
        return signInIdentity;
    }
}